package com.example.wjh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wjh.entity.PlayerData;
import com.example.wjh.entity.Result;
import com.example.wjh.mapper.PlayerDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@CrossOrigin
@RequestMapping("/playerdata")
public class PlayerDataController {
    @Autowired
    private PlayerDataMapper PlayerdataMapper;

    /**
     * 查询所有球员数据
     *
     * @return
     */
    @GetMapping()
    public Result getPlayerdatas() {
        return Result.success(PlayerdataMapper.selectList(null));
    }

    /**
     * 添加球员数据
     *
     * @param Playerdata
     * @return
     */
    @PostMapping()
    public Result createPlayerdata(@RequestBody PlayerData Playerdata) {
        PlayerdataMapper.insert(Playerdata);
        return Result.success();
    }

    /**
     * 更新
     *
     * @param id
     * @param Playerdata
     * @return
     */
    @PutMapping("/{id}")
    public Result updatePlayerdata(@PathVariable("id") int id, @RequestBody PlayerData Playerdata) {
        PlayerData existingPlayerdata = PlayerdataMapper.selectById(id);
        if (existingPlayerdata != null) {
            Playerdata.setId(id);
            PlayerdataMapper.updateById(Playerdata);
            return Result.success();
        } else {
            return null;
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delPlayerdata(@PathVariable("id") int id) {
        System.out.println(id);
        PlayerdataMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/batch")
    public Result delPlayerdataBatch(@RequestBody List<Long> ids) {
        PlayerdataMapper.deleteBatchIds(ids);
        return Result.success();
    }
    /**
     * 模糊查询
     * @param
     * @return
     */
    @GetMapping("/search")
    public Result searchPlayer(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String threepoint,
            @RequestParam(required = false) String offense,
            @RequestParam(required = false) String defense,
            @RequestParam(required = false) String position
    ) {
        return Result.success(PlayerdataMapper.searchPlayerdata(name,threepoint,offense,defense,position));
    }


    /**
     * 分页
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findByPage")
    public Result getPlayerList(@RequestParam("pageNum") Integer pageNum,
                                @RequestParam("pageSize") Integer pageSize) {
        Page<PlayerData> page = new Page<>(pageNum, pageSize);
        QueryWrapper<PlayerData> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id"); // 根据id字段降序排序
        page.addOrder(OrderItem.desc("id")); // 添加降序排序条件
        IPage ipage = PlayerdataMapper.selectPage(page,null);
        return Result.success(ipage);
    }


}

